package com.recsmile.sgs.ui.components;

import com.recsmile.sgs.entity.ClientRoom;
import io.netty.channel.Channel;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Administrator on 2016/8/25.
 */
public class LobbyGui extends JPanel{

    private List<ClientRoom> clientRooms;
    private Channel channel;

    private JPanel roomsPanel ;

    private static final Comparator<ClientRoom> comparator = (o1, o2) -> o1.getRoomId() - o2.getRoomId();

    public LobbyGui(List<ClientRoom> clientRooms, Channel channel) {
        this.clientRooms = clientRooms;
        this.channel = channel;
        setLayout(new BorderLayout());
        Collections.sort(clientRooms, comparator);

        roomsPanel = new JPanel(new GridLayout(10,1));

        for(ClientRoom clientRoom : clientRooms){
            roomsPanel.add(new RoomGui(clientRoom));
        }

        JPanel addRoom = new JPanel(new GridLayout(0,1));
        JButton newRoom = new ControlButtonGui("New Room", e -> {
            //LobbyGui.this.connection.send(new CreateRoomLobbyServerCommand(null, null));
        });
        addRoom.add(newRoom);
        add(roomsPanel, BorderLayout.CENTER);
        add(addRoom, BorderLayout.SOUTH);

    }

    public void updateRoom(ClientRoom clientRoom){
        int index = clientRooms.indexOf(clientRoom);
        if(index == -1){
            clientRooms.add(clientRoom);
            Collections.sort(clientRooms,comparator);
        }else{
            clientRooms.set(index, clientRoom);
        }
        roomsPanel.removeAll();
        for(ClientRoom r : clientRooms){
            roomsPanel.add(new RoomGui(r));
        }
        roomsPanel.revalidate();

    }

    public void remove(ClientRoom r){
        if(clientRooms.remove(r)){
            for(Component component : roomsPanel.getComponents()){
                if(component != null && component instanceof RoomGui && ((RoomGui)component).room.equals(r)){
                    roomsPanel.remove(component);
                    break;
                }
            }
            roomsPanel.revalidate();
        }
    }

    private static class RoomGui extends JButton{
        final ClientRoom room;

        private RoomGui(ClientRoom room) {
            this.room = room;
            final int id = room.getRoomId();
            String name = room.getName();
            int capacity = room.getCapacity();
            int occupancy = room.getPlayers().size();
            boolean chat = room.isChatAllowed();

            JLabel roomID = new LabelGui("Room "+id);
            JLabel nameTag = new LabelGui(name);

            this.setBorder(new LineBorder(Color.BLACK, 2));
            this.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 5));
            add(roomID);
            add(nameTag);
            add(new LabelGui(occupancy+"/"+capacity));
            add(new LabelGui(chat ? "聊天" : "禁聊"));

            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {
                        //connection.send(new EnterRoomLobbyServerCommand(id));
                    }
                }
            });
        }
    }
}
